package lc.q150_200;

public class Q209 {

    public static void main(String[] args) {
        Q209 q209 = new Q209();
        int i = q209.minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3});
        System.out.println(i);
        System.out.println(q209.minSubArrayLen(4, new int[]{1, 4, 4}));
    }

    public int minSubArrayLen(int target, int[] nums) {
        // 双指针法，左指针，右指针
        int min = 0;
        int left = 0;
        int right = 0;
        int sum = 0;
        while (right < nums.length) {
            sum += nums[right];
            while (sum >= target) {
                if (min == 0) {
                    min = right - left + 1;
                } else {
                    min = Integer.min(right - left + 1, min);
                }
                sum -= nums[left];
                left ++;
            }
            right ++;
        }
        return min;
    }

}
